import sys
sys.path.append("D:\\Day\\p2p")
import requests
import pytest
from time import sleep
from api.api_register_login import ApiRegisterLogin

from tools import common_assert, read_json,GetLog

logger = GetLog.get_logger()
class TestRegisterLogin:
    # 初始化
    def setup_method(self):
        self.session = requests.session()
        logger.info("获取session对象{}成功".format(self.session))
        self.api = ApiRegisterLogin(self.session)
        logger.info("获取ApiRegisterLogin对象{}成功".format(self.api))

    # 结束

    def teardown_method(self):
        # 关闭session对象
        self.session.close()
        logger.info("关闭session对象成功")

    # 1、图片验证码接口测试
    @pytest.mark.parametrize("random,except_code",read_json("register_login.json","img_code"))
    def test01_img_code(self,random,except_code):
        try:
            resp = self.api.api_img_code(random)
            logger.info("图片验证码接口测试结果为:{}".format(resp.status_code))
            common_assert(resp,expect_code=except_code)
            logger.info("图片验证码断言成功")
        except Exception as e:
            logger.error(e)
            raise
    # 2、短信验证码接口
    @pytest.mark.parametrize("phone,imgVerifyCode,expect_json",read_json("register_login.json","phone_code"))
    def test02_phone_code(self,phone,imgVerifyCode,expect_json):
        try:
            self.api.api_img_code(123123)
            resp = self.api.api_phone_code(phone,imgVerifyCode)
            logger.info("短信验证码接口测试结果为:{}".format(resp.json()))
            common_assert(resp,expect_json=expect_json)
            logger.info("短信验证码断言成功")
        # 捕获异常的目的就是输出到日志，所以这里需要抛出异常
        except Exception as e:
            logger.error(e)
            raise
    # 3、注册接口 测试
    @pytest.mark.parametrize("phone,password,verifycode,phone_code,dy_server,invite_phone,expect_json",read_json("register_login.json","register"))
    def test03_register(self,phone,password,verifycode,phone_code,dy_server,invite_phone,expect_json):
        try:
            self.api.api_img_code(123123)
            self.api.api_phone_code(19500001111, 8888)
            resp = self.api.api_register(phone, password, verifycode, phone_code,dy_server,invite_phone)
            logger.info("注册接口测试结果为:{}".format(resp.json()))
            common_assert(resp,expect_json=expect_json)
            logger.info("注册接口验证码断言成功")
        except Exception as e:
            logger.error(e)
            raise
    # 4、登录接口 测试
    @pytest.mark.parametrize("keywords,password,expect_json",
                             read_json("register_login.json", "login"))
    def test04_login(self,keywords,password,expect_json):
        try:
            if password == "error123":
                i = 0
                while i < 3:
                    resp = self.api.api_login(keywords, password)
                    logger.info("登录接口测试结果为:{}".format(resp.json()))
                    common_assert(resp, expect_json="错误")
                    logger.info("登录接口验证码断言失败")
                    i+=1
                sleep(60)
                resp = self.api.api_login(keywords, "admin123")
                logger.info("登录接口测试结果为:{}".format(resp.json()))
                common_assert(resp,expect_json=expect_json)
                logger.info("登录接口验证码断言成功")
            else :
                resp = self.api.api_login(keywords, password)
                logger.info("登录接口测试结果为:{}".format(resp.json()))
                common_assert(resp, expect_json=expect_json)
                logger.info("登录接口验证码断言成功")
        except Exception as e:
            logger.error(e)
            raise
    # 5、验证登录状态接口
    @pytest.mark.parametrize("status,expect_json",
                             read_json("register_login.json", "login_status"))
    def test05_login_status(self,status,expect_json):
        try:
            if status=="已登录":
                self.api.api_login("17800001111", "admin123")
            resp = self.api.api_login_status()
            logger.info("验证登录接口测试结果为:{}".format(resp.json()))
            common_assert(resp,expect_json=expect_json)
            logger.info("验证登录接口验证码断言成功")
        except Exception as e:
            logger.error(e)
            raise
